// 1.引入常态化样式表
require("../../assets/css/normalize.css");
// 2.引入重置样式表
require("../../assets/css/reset.css");
// 3.引入公共样式
require("../../assets/css/common.css");
//3.1引入字体图标
require("../../assets/fonts/iconfont.css");
// 4.引入当前页样式
require("./login.less");

//引入js模块
const dom = require("../../utils/dom.js");
const axios = require("../../utils/axios.js");

window.addEventListener("DOMContentLoaded", function () {
	let error = dom.get(".error");

	dom.get("#btn").addEventListener("click", async function () {
		let usnvalue = dom.get("#usn").value;
		//用户名密码验证
		let reg = /^1[356789]\d{9}$/i;
		if (reg.test(usnvalue)) {
			error.textContent = "";
		} else {
			error.textContent = "手机号输入错误";
			return;
		}

		//密码验证
		let psdvalue = dom.get("#psd").value;
		if (psdvalue.length >= 6) {
			error.textContent = "";
		} else {
			error.textContent = "密码至少六位";
			return;
		}

		//所有验证通过,发送后台验证
		let res = await axios.post(
			"http://fitness.h5.itsource.cn:3701/api/user/login",
			{
				account: usnvalue,
				password: psdvalue,
			}
		);

		if (res.data.errno === 0) {
			console.log(res);
			localStorage.setItem("token", res.data.data.token); //将token存储起来以备后面使用
			alert("恭喜您登录成功");
			location.href = "index.html";
		} else {
			alert(res.data.message);
		}
	});
});
